using Brouteforce.NewManagers;
using Brouteforce.UserInterface.Abstracts;
using Brouteforce.UserInterface.Interfaces;
using Brouteforce.UserInterface.UIControls;
using Brouteforce.utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Brouteforce.Screens.PlayingScreen.Panels
{
    public class PlayerStatusPanel : AbstractPanel
    {
        private bool showCreatButton;

        public PlayerStatusPanel(bool toShowCreateButton)
            : base("PlayerStatusPanel")
        {
            showCreatButton = toShowCreateButton;

            //var MenuItemName = "Player\n"+"number";
            //var diffBetweenLines = Draws.CalculateWordSizeNormalFont(MenuItemName);
            //var screenBounds = CalculteCenteredRectangle(panelRectangle, (int)diffBetweenLines.X, (int)Draws.NormalFontSize.Y * 2);
            //Label abstractMenuItem = new Label(MenuItemName, new Rectangle(screenBounds.X, screenBounds.Y, (int)diffBetweenLines.X, (int)diffBetweenLines.Y));
            //AddUIControl(abstractMenuItem);

            /*
            var screenBounds = panelRectangle;
            var Name = "Shooter";
            var img = GameLoop.TileShooter;
            var imgSuroundingRectangle = img.Bounds;
            Image abstractMenuItem = new Image(img, Name, new Rectangle(screenBounds.X, screenBounds.Y,imgSuroundingRectangle.Width,imgSuroundingRectangle.Height));
            AddUIControl(abstractMenuItem);
             * */
        }


        public override void Update(GameTime gameTime)
        {
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Update();
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.GreyStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Draw(gameTime);
            }
        }


        public override void AfterLayout()
        {
            var diffBetweenUIcontents = 15;

            //create new character button
            if (showCreatButton)
            {
                var image = UIControlManager.Instance.CreateButtonTexture;
                Button abstractMenuItem = new Button(image, "Create Button",
                                                     new Rectangle(Bounds.Width - image.Width - diffBetweenUIcontents,
                                                                   (Bounds.Height - image.Height)/2, image.Width,
                                                                   image.Height));
                abstractMenuItem.setOffset(new Vector2(Bounds.X, Bounds.Y));
                abstractMenuItem.OnClick += (() => ScreenManager.Instance.OpenNewCharacterScreen());
                AddUIControl(abstractMenuItem);
            }


            //********************************** "Resources Status" column ******************************************//
            
            //create Image for the player
            var img = UIControlManager.Instance.PlayerTexture;
            Image imageContent = new Image(img, "player Image",
                                                 new Rectangle(diffBetweenUIcontents, diffBetweenUIcontents, img.Width,
                                                       img.Height));
            imageContent.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(imageContent);
            var diffX = img.Width + diffBetweenUIcontents;

            //create "Human Status :" label
            var labelText = "   Human Status";
            var diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            
            int textLabelHight = (int)diffBetweenLines.Y;
            Label label = new Label("Human Status label", labelText,
                                         new Rectangle(diffX+diffBetweenUIcontents, diffBetweenUIcontents, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            //create "      Life :" label
            labelText = "      Life :";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Life label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 2 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Light :" label
            labelText = "      Light:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Light label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 3 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Speed :" label
            labelText = "      Speed:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Speed label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 4 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Power :" label
            labelText = "      Power:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Power label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 5 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //******************************************* "Resources Per Turn" column **********************************//
            int textLabelWidth = (int) diffBetweenLines.X + 30 ;

            //create "Resources Per Turn :" label
            labelText = "   Resources Per Turn";
            textLabelHight = (int)diffBetweenLines.Y;
            label = new Label("Resources Per Turn label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents + textLabelWidth, diffBetweenUIcontents, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            //create "      Life :" label
            labelText = "      Life :";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Life label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents + textLabelWidth, diffBetweenUIcontents + 2 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Light :" label
            labelText = "      Light:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Light label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents + textLabelWidth, diffBetweenUIcontents + 3 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Speed :" label
            labelText = "      Speed:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Speed label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents + textLabelWidth, diffBetweenUIcontents + 4 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);

            //create "      Power :" label
            labelText = "      Power:";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            label = new Label("Power label", labelText,
                                         new Rectangle(diffX + diffBetweenUIcontents + textLabelWidth, diffBetweenUIcontents + 5 * textLabelHight, (int)diffBetweenLines.X,
                                                       textLabelHight));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            label.SetLabelTextColor(Color.Black);
            AddUIControl(label);




        }
    }
}